package fonkin.com.wifisignapp.acticity.aboutMe;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.acticity.sign.ErrorHistory;
import fonkin.com.wifisignapp.acticity.sign.SignApply;
import fonkin.com.wifisignapp.adapter.news.ScheduleAdapter;
import fonkin.com.wifisignapp.bean.schedule.Schedule;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.utils.PrintLog;
import fonkin.com.wifisignapp.utils.ReturnVo;
import fonkin.com.wifisignapp.utils.ShowToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Procedure extends AppCompatActivity {

    LinearLayout qingJia,tiaoXiu;
    String userId="";
    ListView procedureListView;
    ImageView procedureDataBg;
    ScheduleAdapter applicationAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_procedure);
        qingJia = (LinearLayout)findViewById(R.id.qingjia_btn);
        tiaoXiu = (LinearLayout)findViewById(R.id.tiaoxiu_btn);
        procedureListView = (ListView)findViewById(R.id.procedure_listview);
        procedureDataBg = (ImageView) findViewById(R.id.type_no_procedure_history);
        procedureDataBg.setVisibility(View.GONE);
        SharedPreferences preferences =  PreferenceManager.getDefaultSharedPreferences(Procedure.this);
        userId = preferences.getString("user_id","");
        initData(userId,"");
        qingJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData(userId,"");
            }
        });
        tiaoXiu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData(userId,"0");
            }
        });
    }
    public void initData(String userId,String type){
        BaseRetrofit.GetBaseRetrofit().applicationList(userId,type).enqueue(new Callback<ReturnVo<List<Schedule>>>() {
            @Override
            public void onResponse(Call<ReturnVo<List<Schedule>>> call, Response<ReturnVo<List<Schedule>>> response) {
                if (response.isSuccessful()){
                    PrintLog.printApi(response,call);
                    applicationAdapter = new ScheduleAdapter(Procedure.this, new ArrayList<Schedule>());
                    procedureListView.setAdapter(applicationAdapter);
                    if (response.body().getCode().equals(1)){
                        List<Schedule> data = response.body().getData();
                        if (!data.isEmpty()){
                            applicationAdapter.addAll(data);
                        }else{
                            procedureDataBg.setVisibility(View.VISIBLE);
                            Picasso
                                    .with(Procedure.this)
                                    .load(R.drawable.none_data)
                                    .resize(500,400)
                                    .into(procedureDataBg);
                        }

                    }
                }
            }

            @Override
            public void onFailure(Call<ReturnVo<List<Schedule>>> call, Throwable t) {
                ShowToast.ShowToastLong("ncdsojodsi",Procedure.this);
            }
        });

    }

    public void addApplication(View view ){
        Intent intent = new Intent(Procedure.this, SignApply.class);
        startActivity(intent);
    }
}
